package com.ssm.service.imp;

import com.ssm.service.EmailCode;
import com.ssm.utils.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.FileNotFoundException;

@Service("emailCodeImp")
public class EmailCodeImp implements EmailCode {


    @Autowired
    private  HttpServletRequest request;

    @Override
    public String registeredEmailCode(String[] charge) throws FileNotFoundException {
        Object emailCode = request.getSession().getAttribute("emailCode");
        if(emailCode == null){
            emailCode = (int)((Math.random()*9+1)*100000);
        }
        MailService mail = new MailService();
        mail.setSubject("邮箱验证码");
        mail.setContent("邮箱验证码是：" + emailCode);
        //收件人 可以发给其他邮箱(163等) 下同
        mail.setTo(charge);
//        //抄送
//        mail.setCc(new String[] {"xxx@qq.com","xxx@qq.com"});
//        //密送
//        mail.setBcc(new String[] {"xxx@qq.com","xxx@qq.com"});
//        //发送附件列表 可以写绝对路径 也可以写相对路径(起点是项目根目录)
//        mail.setFileList(new String[] {"file\\附件1.txt","file\\附件2.txt"});
        //发送邮件
        try {
            mail.sendMessage();
            System.out.println("发送邮件成功！");
            return Integer.toString((int)emailCode);
        } catch (Exception e) {
            System.out.println("发送邮件失败！");
            e.printStackTrace();
            return null;
        }
    }
}
